package com.cfg;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;

import com.alibaba.druid.filter.Filter;
import com.alibaba.druid.filter.logging.Slf4jLogFilter;
import com.alibaba.druid.pool.DruidDataSource;

@Configuration
@EnableJpaAuditing
@EnableConfigurationProperties({
	DruidDSConfig.DruidDataSourcProperties.class})
public class DruidDSConfig {

	@Autowired
	private DruidDataSourcProperties dsProperties;
	
	@Bean
	public DataSource dataSource() {
		return createDataSource(dsProperties);
	}
	
	private DataSource createDataSource(BaseDataSourcProperties properties) {
		DruidDataSource dataSource = null;
		try {
			dataSource = new DruidDataSource();
			dataSource.setUrl(properties.getUrl());
			dataSource.setUsername(properties.getUsername());
			dataSource.setPassword(properties.getPassword());
			dataSource.setFilters(properties.getFilters());
			dataSource.setInitialSize(properties.getInitialSize());
			dataSource.setMaxActive(properties.getMaxActive());
			dataSource.setMinIdle(properties.getMinIdle());
			Slf4jLogFilter log=new com.alibaba.druid.filter.logging.Slf4jLogFilter();
			log.setStatementExecutableSqlLogEnable(true);
//			log.set
			List<Filter> logList=new ArrayList<>();
			logList.add(log);
			dataSource.setProxyFilters(logList);
		} catch (SQLException e) {
			System.err.println(e);
		}
		return dataSource;
	}
	
	public static class BaseDataSourcProperties {
		/**
		 * JDBC url of the database.
		 */
		private String url;

		/**
		 * Login user of the database.
		 */
		private String username;

		/**
		 * Login password of the database.
		 */
		private String password;
		
		/**
		 * 插件
		 */
		private String filters;
		
		/**
		 * 最大并发连接数
		 */
		private int maxActive;
		
		/**
		 * 初始化连接数量
		 */
		private int initialSize;
		
		/**
		 * 最小空闲连接数
		 */
		private int minIdle;

		public String getUrl() {
			return url;
		}

		public void setUrl(String url) {
			this.url = url;
		}

		public String getUsername() {
			return username;
		}

		public void setUsername(String username) {
			this.username = username;
		}

		public String getPassword() {
			return password;
		}

		public void setPassword(String password) {
			this.password = password;
		}

		public String getFilters() {
			return filters;
		}

		public void setFilters(String filters) {
			this.filters = filters;
		}

		public int getMaxActive() {
			return maxActive;
		}

		public void setMaxActive(int maxActive) {
			this.maxActive = maxActive;
		}

		public int getInitialSize() {
			return initialSize;
		}

		public void setInitialSize(int initialSize) {
			this.initialSize = initialSize;
		}

		public int getMinIdle() {
			return minIdle;
		}

		public void setMinIdle(int minIdle) {
			this.minIdle = minIdle;
		}
		
	}
	
	@ConfigurationProperties(prefix = "druid.datasource")
	public static class DruidDataSourcProperties extends BaseDataSourcProperties {}
}